/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.builder.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.UnknownTypeHandler;

import com.mapperdb.helper.Annt;
import com.mapperdb.helper.AnntHandler;
import com.mapperdb.helper.AnntHelper;
import com.mapperdb.type.JavaType;
import com.mapperdb.util.MapperUtils;

/**
 * Result 注解 生成
 * 
 * @author Administrator
 *
 */
public class ResultBuilder {
	public static final Pattern GET_PATTERN = Pattern.compile("^(get|is)([A-Z]+)([A-Za-z0-9_]*)");
	protected List<Result> results = new ArrayList<Result>();

	public ResultBuilder() {}

	public ResultBuilder(List<Method> methods) {
		parseResults(methods);
	}

	public static ResultBuilder use(List<Method> methods) {
		return new ResultBuilder(methods);
	}

	public void parseResult(Method method) {
		if (!MapperUtils.isAttr(method)) return;
		Boolean id = false;
		String column = "";
		String property = "";
		Class<?> javaType = Void.TYPE;
		JdbcType jdbcType = JdbcType.UNDEFINED;
		Class<? extends TypeHandler<?>> typeHandler = UnknownTypeHandler.class;
		One one = null;
		Many many = null;

		Class<? extends Annotation> resultAnnotationClass = AnntHelper.getResultAnnotationType(method);
		if (resultAnnotationClass != null) {
			Annt annt = Annt.use(method, resultAnnotationClass);

			id = annt.get("id");
			column = annt.get("column");
			property = annt.get("property");
			javaType = annt.get("javaType");
			jdbcType = annt.get("jdbcType");
			typeHandler = annt.get("typeHandler");
			one = annt.get("one");
			many = annt.get("many");
		}

		if (property.isEmpty()) property = getAttrName(method);
		if (column.isEmpty()) column = property;
		if (javaType == Void.TYPE) javaType = method.getReturnType();
		if (jdbcType == JdbcType.UNDEFINED) jdbcType = JavaType.forCode(javaType);
		if (typeHandler == UnknownTypeHandler.class) typeHandler = null;

		Result result = AnntHandler.use(Result.class)//
				.set("id", id)//
				.set("column", column)//
				.set("property", property)//
				.set("javaType", javaType,false)//
				.set("one", one)//
				.set("many", many)//
				.set("jdbcType", jdbcType, false)//
				.set("typeHandler", typeHandler, false)//
				.newProxyInstance();
		results.add(result);
	}

	public Result[] getResults() {
		return results.toArray(new Result[0]);
	}

	protected void parseResults(List<Method> methods) {
		for (Method method : methods) {
			this.parseResult(method);
		}
	}

	/**
	 * @Title: 获得属性名称(属性已过滤就不做其它处理了)
	 * @param method
	 * @return
	 */
	protected String getAttrName(Method method) {
		Matcher m = GET_PATTERN.matcher(method.getName());
		m.find();
		return m.group(2).toLowerCase() + m.group(3);
	}

}
